package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.OptionMapper;
import com.tanhua.dubbo.mappers.QuestionnaireMapper;
import com.tanhua.dubbo.mappers.SoulQuestionMapper;
import com.tanhua.model.domain.Option;
import com.tanhua.model.domain.Questionnaire;
import com.tanhua.model.domain.SoulQuestion;
import com.tanhua.model.vo.OptionVo;
import com.tanhua.model.vo.QuestionVo;
import com.tanhua.model.vo.QuestionnaireVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author fff
 * @date 2021/12/11 13:20
 **/
@DubboService
public class QuestionnaireApiImpl implements QuestionnaireApi{
    @Autowired
    private QuestionnaireMapper questionnaireMapper;
    @Autowired
    private SoulQuestionMapper soulQuestionMapper;
    @Autowired
    private OptionMapper optionMapper;


    @Override
    public List find() {
        //查出所有的数据
        QueryWrapper<Questionnaire> qw = new QueryWrapper<>();
        List<Questionnaire> list = questionnaireMapper.selectList(qw);
        List<QuestionnaireVo> Vos = new ArrayList<>();

        List<QuestionVo> questions = new ArrayList<>();


        for (Questionnaire q : list) {
            //根据问卷id找对应的题目
            String id = q.getId();
            QueryWrapper<SoulQuestion> qw1 = new QueryWrapper<>();
            qw1.eq("questionnaire_id",id);
            List<SoulQuestion> questionList = soulQuestionMapper.selectList(qw1);

            for (SoulQuestion question : questionList) {
                //根据题目id找对应的选项
                String id1 = question.getId();
                QueryWrapper<Option> qw2 = new QueryWrapper<>();
                qw2.eq("question_id",id1);
                List<Option> options = optionMapper.selectList(qw2);

                List<OptionVo> optionVos = new ArrayList<>();
                for (Option option : options) {
                    OptionVo optionVo = new OptionVo(option.getId(),option.getContent());

                    optionVos.add(optionVo);
                }

                QuestionVo questionVo = QuestionVo.init(question);
                questionVo.setOptions(optionVos);

                questions.add(questionVo);
            }

            QuestionnaireVo init = QuestionnaireVo.init(q);
            init.setQuestions(questions);

            Vos.add(init);

        }



        return Vos;
    }
}
